package com.xmler.plugin.adbrocket.adb.command

import com.android.ddmlib.IDevice
import com.intellij.openapi.project.Project
import com.xmler.plugin.adbrocket.adb.command.receiver.DisplayReceiver
import com.xmler.plugin.adbrocket.utils.AdbUtil
import com.xmler.plugin.adbrocket.utils.Logger
import org.jetbrains.android.facet.AndroidFacet
import java.util.concurrent.TimeUnit
import javax.swing.JTextArea

class KillCmd : ICommand {
    override fun run(project: Project, device: IDevice, facet: AndroidFacet, packageName: String, display: JTextArea): Boolean {
        try {
            if (AdbUtil.isAppInstalld(device, packageName)) {
                device.executeShellCommand(
                    "am force-stop $packageName",
                    DisplayReceiver(display),
                    10L,
                    TimeUnit.SECONDS
                )
                Logger.d("$packageName forced-stop on ${device.name}")
                return true
            } else {
                Logger.e("$packageName is not installed on ${device.name}")
            }
        } catch (e: Exception) {
            Logger.e("Kill cmd error: ${e.message}")
        }
        return false
    }

}
